package com.itxiaowu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itxiaowu.constant.MessageConstant;
import com.itxiaowu.entity.Result;
import com.itxiaowu.pojo.OrderSetting;
import com.itxiaowu.service.OrderSettingService;
import com.itxiaowu.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *  预约管理
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 1:上传报表文件
     */
    @RequestMapping("/upload.do")
    public Result uploadPoi(@RequestParam("excelFile") MultipartFile excelFile){

        //1:调用POI工具类获得数据
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            if(list != null && list.size() > 0) {

                //2:将数据赋值给相应的类
                List<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] strings : list) {

                    //将数据转换为OrderSetting 对象
                    OrderSetting orderset = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
                    //将转换后的数据添加进入集合中
                    orderSettingList.add(orderset);

                }
                //3:调用service层进行数据上传
                orderSettingService.upload(orderSettingList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }


    /**
     *  查询页面数据
     */
    @RequestMapping("/getMsgByMonth.do")
    public Result getMsgByMonth(String date){

        try{
            //查询开始条件及结束条件
            String start = date + "-1";
            String end = date + "-31";

            //System.out.println(start + end);
            List<Map<String, Object>> list = orderSettingService.getMsgByMonth(start, end);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,list);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     *  设置可预约人数
     * @param orderSetting
     * @return
     */
    @RequestMapping("/setnumber.do")
//    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result setNumber(@RequestBody OrderSetting orderSetting){

        try{
            orderSettingService.setNumber(orderSetting);
        }catch (Exception e){
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);

    }

//    public static void main(String[] args) {
//
//        String str = "2019/02/03";
//        Date date = new Date(str);
//        System.out.println(date);
//
//    }

}
